package com.weichat.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Event_user;
import com.model.Propertr_user;
import com.model.User;
import com.service.Event_userService;
import com.service.Propertr_userService;
import com.service.UserService;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.Sha1Util;
import com.weichat.tool.WeiChatConfig;

@RestController
public class getUserController {
	
	@Autowired
	UserService ud;  
	@Autowired
	AccessTokentool accessTokentool;
	@Autowired
	Event_userService event_userService;
	@Autowired
	Propertr_userService propertr_userService;
	@RequestMapping(value="/login1",method=RequestMethod.POST)
	public Map login1(@RequestParam("name") String userName,@RequestParam("password") String pwd,HttpServletRequest req,HttpServletResponse response) throws UnsupportedEncodingException{
		req.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		System.out.println("userName"+userName+"--pwd--"+pwd);
		
		Map r=new HashMap();
		//System.out.println(U1);
		String m;
		User U1=ud.selectByName(userName).get(0);
		if(U1!=null){
			if(U1.getPassword().equals(pwd)){
				m="Y";
				//m.put("url", (String)req.getSession().getAttribute("url"));
				U1.setOpenid((String)req.getSession().getAttribute("openId"));
				ud.updateByPrimaryKeySelective(U1);
				
			}else{
				m="S";
			}
			
		}else{
			m="N";
		}
		System.out.println("bsbsbsbsbbsbsbbbsbsbsbbsbs"+m);
		r.put("rs", m);
		return r;
	}
	
	@RequestMapping(value="/123register112312412",method=RequestMethod.POST)
	public Map register1(@RequestParam("name") String userName,@RequestParam("password") String pwd,@RequestParam("email") String email, @RequestParam("phone") String phone, @RequestParam("classNumber") String clazzId,@RequestParam("type") int type,HttpServletRequest req,HttpServletResponse response) throws UnsupportedEncodingException{
		
			req.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
	
		
		System.out.println("userName"+userName+"--pwd--"+pwd+"email---"+email+"phone---"+phone);		
		Map m=new HashMap();
		
		Map r=new HashMap();
		User u=new User();
		u.setName(userName);
		u.setPassword(pwd);
		u.setEmail(email);
		u.setTel(phone);
		u.setClazzid(clazzId);
		String openId=(String) req.getSession().getAttribute("openId");
		u.setOpenid(openId);
		u.setType(type);
		System.out.println("SIZE============="+ud.selectByName(userName).size());
		String fq;
		if(ud.selectByName(userName).size()==0){
			ud.insertSelective(u);
			HttpSession session = req.getSession();
			User u1=ud.selectByOpenId(openId).get(0);
			session.setAttribute("user", u1);
			 fq="Y";
		}else{
			 fq="N";
		}
		System.out.println("bsbsbsbsbbsbsbbbsbsbsbbsbs"+fq);
		r.put("rs", fq);
		return r;
		}
	
	@RequestMapping("/lswConfig")
	public Map<String,String> getJSDKconfig(@RequestParam("url") String url) throws IOException{
		Map<String,String> configParam=new HashMap<String,String>();
		Sha1Util su=new Sha1Util();
		String accesstoken=(String) accessTokentool.getNewAccessToken().get("rs");
		String Ticket=su.getjsApiTicket("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=",accesstoken);
		String NonceStr=su.getNonceStr();
		String timestamp=su.getTimeStamp();
		System.out.println("url======="+url);
		String signature=su.getSha1("jsapi_ticket="+Ticket+"&noncestr="+NonceStr+"&timestamp="+timestamp+"&url="+url);
		
		
		
		configParam.put("appId", WeiChatConfig.appid);
		configParam.put("timestamp", timestamp);
		configParam.put("nonceStr", NonceStr);
		configParam.put("signature", signature);
		
		
		
		return configParam;
	}
	
	@RequestMapping("/eveningSetPhone")
	public String eveningSetPhone(HttpServletRequest request) throws Exception{
		
		String rs = null;
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
        String dateNowStr = sdf.format(now.getTimeInMillis()); 
  
        String openId=(String) request.getSession().getAttribute("openId");
        User u=(User) request.getSession().getAttribute("user");
        
        Event_user eu=new Event_user();
		
		eu.setEventId(11);
		eu.setType("Y");
		eu.setUserId(u.getId());
		
		
		Propertr_user pu1=new Propertr_user();
		pu1.setCount(100);
		pu1.setpId(2);
		pu1.setuId(u.getId());
		pu1.setEventName("晚上放手机");
		Propertr_user pu2=new Propertr_user();
		pu2.setCount(100);
		pu2.setpId(3);
		pu2.setuId(u.getId());
		pu2.setEventName("晚上放手机");
		event_userService.insertSelective(eu);
		int euId=event_userService.selectEidByUidOrder(u.getId()).getEuId();
		System.out.println("euId------"+euId);
		pu1.setEuId(euId);
		pu2.setEuId(euId);
		if((0<propertr_userService.insertSelective(pu1)&&(0<propertr_userService.insertSelective(pu2)))){
			
			
			rs="Y";
		}
		
		
		
		return rs;
	}


}
